<template>
  <el-container class="admin-layout">
    <!-- 顶部导航 -->
    <el-header class="admin-header">
      <div class="header-left">
        <h2>🎫 娱乐票务管理系统</h2>
      </div>
      <div class="header-right">
        <el-dropdown trigger="click" placement="bottom-end" popper-class="admin-dropdown">
          <span class="el-dropdown-link">
            <el-icon><User /></el-icon>
            {{ adminName || '管理员' }}
            <el-icon class="el-icon--right"><arrow-down /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="goSettings">个人设置</el-dropdown-item>
              <el-dropdown-item divided @click="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>

    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px" class="admin-aside">
        <el-menu
          default-active="1"
          class="admin-menu"
          router
          :collapse="false"
        >
          <el-menu-item index="/">
            <el-icon><House /></el-icon>
            <span>首页</span>
          </el-menu-item>
          
          <el-sub-menu index="2">
            <template #title>
              <el-icon><Tickets /></el-icon>
              <span>票务管理</span>
            </template>
            <el-menu-item index="/tickets">票务列表</el-menu-item>
            <el-menu-item index="/events">活动管理</el-menu-item>
          </el-sub-menu>

          <el-sub-menu index="3">
            <template #title>
              <el-icon><User /></el-icon>
              <span>用户管理</span>
            </template>
            <el-menu-item index="/users">用户列表</el-menu-item>
            <el-menu-item index="/orders">订单管理</el-menu-item>
          </el-sub-menu>

          <el-menu-item index="/analytics">
            <el-icon><DataAnalysis /></el-icon>
            <span>数据分析</span>
          </el-menu-item>

          <el-menu-item index="/settings">
            <el-icon><Setting /></el-icon>
            <span>系统设置</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 主内容区 -->
      <el-main class="admin-main">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { authAPI } from '@/api'
export default {
  name: 'AdminLayout',
  data() {
    return { adminName: '' }
  },
  methods: {
    async fetchAdmin() {
      try {
        const info = await authAPI.getAdminInfo()
        this.adminName = info?.username || info?.name || '管理员'
        localStorage.setItem('admin_info', JSON.stringify(info || {}))
      } catch (_) {
        const cached = localStorage.getItem('admin_info')
        if (cached) {
          try { this.adminName = JSON.parse(cached)?.username || '管理员' } catch (_) {}
        }
      }
    },
    goSettings() {
      this.$router.push('/settings')
    },
    async logout() {
      try { await authAPI.logout() } catch (_) {}
      localStorage.removeItem('admin_token')
      localStorage.removeItem('admin_info')
      this.$router.push('/login')
    }
  },
  mounted() {
    this.fetchAdmin()
  }
}
</script>

<style scoped>
.admin-layout {
  /* 定义头部高度变量，便于不同分辨率下统一计算 */
  --header-height: 60px;
  height: 100vh;
  width: 100vw;
  margin: 0;
  padding: 0;
  position: fixed;
  top: 0;
  left: 0;
  overflow: hidden;
}

.admin-header {
  background-color: #409eff;
  color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: var(--header-height);
  flex-shrink: 0;
}

.header-left h2 {
  margin: 0;
  font-size: 18px;
}

.header-right {
  color: white;
}

.el-dropdown-link {
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 8px;
  background: rgba(255,255,255,0.14);
  transition: background .2s ease;
}
.el-dropdown-link:hover {
  background: rgba(255,255,255,0.22);
}

.admin-aside {
  background-color: #f5f5f5;
  width: 200px !important;
  flex-shrink: 0;
}

.admin-menu {
  border-right: none;
  height: 100%;
  width: 100%;
}

/* 第二层容器（侧栏 + 主内容）占据剩余高度，确保内部滚动生效 */
.admin-layout > .el-container {
  height: calc(100vh - var(--header-height));
}

.admin-main {
  background-color: #f0f2f5;
  flex: 1;
  height: 100%;
  overflow-y: auto;
  padding: 20px;
}

/* 下拉菜单统一样式，避免首次悬浮显示不规则外框 */
:deep(.admin-dropdown) {
  padding: 0 !important;
}
:deep(.admin-dropdown .el-dropdown-menu) {
  border-radius: 10px;
  box-shadow: 0 10px 24px rgba(0,0,0,0.12);
  overflow: hidden;
}
:deep(.admin-dropdown .el-dropdown-menu__item) {
  padding: 10px 14px;
}

/* 4K屏幕适配 */
@media screen and (min-width: 2560px) {
  .admin-layout { --header-height: 80px; }
  .admin-aside {
    width: 280px !important;
  }
  
  .admin-header {
    padding: 0 40px;
  }
  
  .header-left h2 {
    font-size: 24px;
  }
  
  .admin-main {
    padding: 40px;
  }
}

@media screen and (min-width: 3840px) {
  .admin-layout { --header-height: 100px; }
  .admin-aside {
    width: 320px !important;
  }
  
  .admin-header {
    padding: 0 60px;
  }
  
  .header-left h2 {
    font-size: 28px;
  }
  
  .admin-main {
    padding: 60px;
  }
}
</style>